" 基本设置
set nocompatible "关闭与vi的兼容模式
set number "显示行号
set nowrap    "不自动折行
set showmatch    "显示匹配的括号
set scrolloff=3        "距离顶部和底部3行"
set encoding=utf-8  "编码
set fenc=utf-8      "编码
set fileencodings=utf-8
set hlsearch        "搜索高亮
syntax on    "语法高亮
set tabstop=4   "tab宽度
set shiftwidth=4  
set smarttab
set backspace=indent,eol,start
set expandtab       "tab替换为空格键
set fileformat=unix   "保存文件格式
set splitbelow
set cmdheight=2
set completeopt=longest,menu
set splitright
set foldmethod=indent
set foldlevel=99

" 在普通模式下，按r来运行Python脚本
noremap r :call RunPython()<CR>
func! RunPython()
    exec "w"
    if &filetype == 'python'
        exec "!time python %"
    endif
endfunc

" vim-plug插件管理
call plug#begin('~/.config/nvim/plugged')
Plug 'junegunn/vim-plug'
" git支持
Plug 'tpope/vim-fugitive'
" Python自动缩进插件
Plug 'vim-scripts/indentpython.vim'
" 项目管理工具
Plug 'mhinz/vim-startify'
" 快速对齐插件
Plug 'junegunn/vim-easy-align'
" 当前光标下的单词增加下划线
Plug 'itchyny/vim-cursorword'
" 快速选择插件
Plug 'tpope/vim-surround'
" 自定义代码片断
Plug 'honza/vim-snippets'
" 语法高亮支持
Plug 'sheerun/vim-polyglot'
" 主题、状态栏设置
Plug 'haishanh/night-owl.vim'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'ryanoasis/vim-devicons'
" coc扩展
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" fzf模糊查找
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
" whichkey快捷菜单
Plug 'liuchengxu/vim-which-key
" 浮动窗口支持
Plug 'voldikss/vim-floaterm'
" ranger文件管理器支持
Plug 'kevinhwang91/rnvimr'
call plug#end()
" 启用标签栏
let g:airline#extensions#tabline#enabled = 1
" 支持图标字体
let g:airline_powerline_fonts = 1
" 设置状态栏主题
let g:airline_theme='night_owl'
" 设置主题
set termguicolors
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
syntax enable
colorscheme night-owl

" 设置菜单背景
highlight Pmenu ctermbg=black guibg=black
highlight Pmenu ctermfg=blue guibg=blue
highlight PmenuSel ctermfg=black guifg=black
highlight PmenuSel ctermbg=blue guibg=blue

" coc扩展
let g:coc_global_extensions = [
      \'coc-json',
      \'coc-pyright',
      \'coc-snippets',
      \'coc-xml',
      \'coc-explorer',
      \'coc-prettier',
      \'coc-highlight']

" 使用tab键进行补全选择
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

" 设置空格为leader键
let mapleader=" "

" 使用回车进行补全选择
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')

" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocAction('format')

" Add `:Fold` command to fold current buffer.
command! -nargs=? Fold :call     CocAction('fold', <f-args>)

" Add `:OR` command for organize imports of the current buffer.
command! -nargs=0 OR   :call     CocAction('runCommand', 'editor.action.organizeImport')

" 添加状态栏显示支持
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

" 启用Prettier进行文件自动格式化
command! -nargs=0 Prettier :CocCommand prettier.formatFile
let g:prettier#autoformat = 1

" 设置Coc Search
nnoremap <leader>? :CocSearch <C-R>=expand("<cword>")<CR><CR>

" 将空格设置为whichkeyleader键
nnoremap <silent> <leader> :WhichKey '<Space>'<CR>

" whichkey基本配置
let g:which_key_timeout = 100
let g:which_key_display_names = {'<CR>': '↵', '<TAB>': '⇆'}
let g:which_key_map =  {}
let g:which_key_sep = '→'
let g:which_key_use_floating_win = 0
let g:which_key_max_size = 0

" 呼出whichkey时隐藏状态栏
autocmd! FileType which_key
autocmd  FileType which_key set laststatus=0 noshowmode noruler
  \| autocmd BufLeave <buffer> set laststatus=2 noshowmode ruler

" 自定义whichkey
let g:which_key_map['?'] = 'search word'
let g:which_key_map['/'] = [ ':call Comment()'                                 , 'comment' ]
let g:which_key_map['.'] = [ ':e $MYVIMRC'                                     , 'open init' ]
let g:which_key_map[';'] = [ ':Commands'                                       , 'commands' ]
let g:which_key_map['e'] = [ ':CocCommand explorer --toggle --sources=file+'   , 'explorer' ]
let g:which_key_map['n'] = [ ':let @/ = ""'                                    , 'no highlight' ]
let g:which_key_map['q'] = [ '<Plug>(coc-fix-current)'                         , 'quickfix' ]
let g:which_key_map['u'] = [ ':UndotreeToggle'                                 , 'undo tree']
let g:which_key_map['t'] = [':FloatermNew --wintype=normal --height=6'        , 'terminal']
let g:which_key_map['r'] = [ ':FloatermNew ranger'                            , 'ranger']
let g:which_key_map['f'] = [':FloatermNew fzf'                               , 'fzf']
let g:which_key_map['g'] = [':FloatermNew lazygit'                           , 'git']
let g:which_key_map['p'] = [':FloatermNew python'                            , 'python']
call which_key#register('<Space>', "g:which_key_map")